﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.BLL;
using Maticsoft.Model;

namespace FineUI.Examples
{
    public partial class student_download_template : System.Web.UI.Page
    {

        protected DataSet LoadTemplateInform()
        {
            string currentS_Id=Session["UserName"].ToString();
            Maticsoft.BLL.ReportTemplate documentTemplate = new Maticsoft.BLL.ReportTemplate();
            DataSet DSTemplate = documentTemplate.GetTemplateInfrom("SelectCourse.TC_Id=TeachingClass.TC_Id and TeachingClass.T_Id=Teacher.T_Id and TeachingClass.TC_Id=ReportTemplate.TC_Id and R_Status='已发布' and SelectCourse.S_Id='" + currentS_Id +"'");

            for (int i = 0; i < DSTemplate.Tables[0].Rows.Count; i++)
            {
                DSTemplate.Tables[0].Rows[i]["TC_Date_Day"] = DSTemplate.Tables[0].Rows[i]["TC_Date_Day"] + " 第" + DSTemplate.Tables[0].Rows[i]["TC_Date_Time"]+"节课";
            }


            DataView DVTemplate = DSTemplate.Tables[0].DefaultView;
            Grid1.DataSource = DVTemplate;
            Grid1.DataBind();

            return DSTemplate;  
        }

        protected void ExistsPermiss(string P_Id, string P_Status)
        {
            Maticsoft.BLL.Permissions PM = new Maticsoft.BLL.Permissions();
            if (!PM.ExistsPermiss(P_Id, P_Status))
            {
                Response.Redirect("~/user_no_permission.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
            }
            ExistsPermiss("S003", "已授权");
            if (!IsPostBack)
            {
                LoadTemplateInform();
            }
        }

        protected void DocDownLoad(string current_R_Id)
        {
            string currentUser = Session["UserName"].ToString();
            Maticsoft.BLL.ReportTemplate documentation = new Maticsoft.BLL.ReportTemplate();
            Maticsoft.Model.ReportTemplate documentInform = documentation.GetModel(current_R_Id);
            string fileRpath = Server.MapPath(@"~/document/teacher/") + documentInform.R_Path;

            Response.ClearHeaders();
            Response.Clear();
            Response.Expires = 0;
            Response.Buffer = true;
            Response.AddHeader("Accept-Language", "zh-tw");
            string name = documentInform.R_Formerly.ToString();
            System.IO.FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] byteFile = new byte[files.Length];

            files.Read(byteFile, 0, byteFile.Length);
            files.Close();
            Response.ContentType = "application/octet-stream;charset=gbk";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));

            Response.BinaryWrite(byteFile);
            Response.Flush();
            Response.End();


        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadTemplateInform();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionDownLoad")//下载
            {
                int key = e.RowIndex;
                string CurrentID = LoadTemplateInform().Tables[0].Rows[key]["R_Id"].ToString();
                DocDownLoad(CurrentID);
                //DataBind().Table[0].Row[key]["R_Id"].ToString();
            }
            if (e.CommandName == "ActionPreview")//下载
            {
                int key = e.RowIndex;
                string current_R_Id = LoadTemplateInform().Tables[0].Rows[key]["R_Id"].ToString();

                Maticsoft.BLL.ReportTemplate documentation = new Maticsoft.BLL.ReportTemplate();
                Maticsoft.Model.ReportTemplate documentInform = documentation.GetModel(current_R_Id);
                string fileRpath = Server.MapPath(@"~/document/teacher/") + documentInform.R_Path;

                Session["Doc_Path"] = fileRpath;
                Session["Doc_Flag"] = "teacher";
                Response.Redirect("~/user_view_document.aspx");
              //  DocDownLoad(CurrentID);
                //DataBind().Table[0].Row[key]["R_Id"].ToString();
            }

        }
    }
}